package com.xqls.server.dto.recive;

import com.xqls.biz.SokectBiz;
import com.xqls.server.dto.SoketCommonDto;
import com.xqls.server.netty.DecoderUtil;
import com.xqls.util.ContextUtil;
import com.xqls.util.Retaindecimal;
import com.xqls.util.Retaindecimal;
import io.netty.buffer.ByteBuf;
import org.apache.log4j.Logger;
import java.util.Date;

/**
 * 采样仪采样记录
 * 采样仪主动上传：
 */
public class SamplingDto extends SoketCommonDto {
    private static Logger logger = Logger.getLogger(SamplingDto.class);
    private static final String  TYPE_SAMPLE="fe";//模拟触发采样记录

    private static final String  TYPE_OTHER="ff";//其他模式采样记录
    
    /**
     * 液位
     * 0代表无液位,停止排水;1代表有液位,开始排水
     */
    private Integer level;

    /**
     * 仪器门状态，0代表关，1代表开
     */
    private Integer valveStatus;
    /**
     * 仪器前一次采样的进水状态，0代表进水成功，1代表进水失败
     */
    private Integer inletStatus;
    /**
     * 仪器电源状态，0代表供电正常，1代断电
     */
    private Integer powerStatus;
    /**
     * 仪器是否启动，0空闲，1启动
     */
    private Integer startUp;

    /**
     * 冰箱温度，字节的bit8=1表示负温度，bit8=0表示正温度
     */
    private Integer temperature;

    private Double UV;

    private Double SS;

    private Double NH3;

    private Double PH;

   /**
        采样瓶号
     */
    private Integer number;
   /**
          采样瓶次
       */
    private Integer frequency;
   /**
            采样量
         */
    private Integer  measure;

   /**
       当前瓶水样总容量
           */
    private Integer  totalMeasure;

    /**
     触发采样时的温度值
     */
    private Double  accumulativeTotal;

    /**
     触发采样记录模式
     */
    private String  type;

    /**
     * 采样记录时间
     */
    private Date  time;


    public Integer getValveStatus() {
        return valveStatus;
    }

    public void setValveStatus(Integer valveStatus) {
        this.valveStatus = valveStatus;
    }

    public Integer getInletStatus() {
        return inletStatus;
    }

    public void setInletStatus(Integer inletStatus) {
        this.inletStatus = inletStatus;
    }

    public Integer getPowerStatus() {
        return powerStatus;
    }

    public void setPowerStatus(Integer powerStatus) {
        this.powerStatus = powerStatus;
    }

    public Integer getStartUp() {
        return startUp;
    }

    public void setStartUp(Integer startUp) {
        this.startUp = startUp;
    }

    public Integer getTemperature() {
        return temperature;
    }

    public void setTemperature(Integer temperature) {
        this.temperature = temperature;
    }

    public Double getUV() {
        return UV;
    }

    public void setUV(Double UV) {
        this.UV = UV;
    }

    public Double getSS() {
        return SS;
    }

    public void setSS(Double SS) {
        this.SS = SS;
    }

    public Double getNH3() {
        return NH3;
    }

    public void setNH3(Double NH3) {
        this.NH3 = NH3;
    }

    public Double getPH() {
        return PH;
    }

    public void setPH(Double PH) {
        this.PH = PH;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Integer getFrequency() {
        return frequency;
    }

    public void setFrequency(Integer frequency) {
        this.frequency = frequency;
    }

    public Integer getMeasure() {
        return measure;
    }

    public void setMeasure(Integer measure) {
        this.measure = measure;
    }

    public Integer getTotalMeasure() {
        return totalMeasure;
    }

    public void setTotalMeasure(Integer totalMeasure) {
        this.totalMeasure = totalMeasure;
    }

    public Double getAccumulativeTotal() {
        return accumulativeTotal;
    }

    public void setAccumulativeTotal(Double accumulativeTotal) {
        this.accumulativeTotal = accumulativeTotal;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }


    public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	protected void decode(ByteBuf in,SoketCommonDto soketCommonDto){
        Retaindecimal retaindecimal=new Retaindecimal();
        String bitStr= DecoderUtil.byteToBit(in.readByte());
        logger.info(bitStr);
        SamplingDto samplingDto=(SamplingDto)soketCommonDto;
        samplingDto.setValveStatus(DecoderUtil.bitToInteger(bitStr,7));
        samplingDto.setInletStatus(DecoderUtil.bitToInteger(bitStr,6));
        samplingDto.setLevel(DecoderUtil.bitToInteger(bitStr,5));
        samplingDto.setPowerStatus(DecoderUtil.bitToInteger(bitStr,4));
        samplingDto.setStartUp(DecoderUtil.bitToInteger(bitStr,2));
        String temperatureSt =DecoderUtil.byteToBit(in.readByte());
        Integer temperatureBit=DecoderUtil.bitToInteger(temperatureSt,0);
        Integer temperature=   Integer.valueOf(temperatureSt,2);
        if(temperatureBit.intValue()==1){  //字节的bit8=1表示负温度，bit8=0表示正温度
            temperature=-temperature;
        }
        samplingDto.setTemperature(temperature);

        samplingDto.setUV(retaindecimal.divideTen(DecoderUtil.shortToInt(in.readShort())));
        samplingDto.setSS(retaindecimal.divideTen(DecoderUtil.shortToInt(in.readShort())));
        samplingDto.setNH3(retaindecimal.divideHundred(DecoderUtil.shortToInt(in.readShort())));
        samplingDto.setPH(retaindecimal.divideHundred(DecoderUtil.shortToInt(in.readShort())));
        samplingDto.setNumber(DecoderUtil.byteToInt(in.readByte()));
        samplingDto.setFrequency(DecoderUtil.shortToInt(in.readShort()));
        samplingDto.setMeasure(DecoderUtil.shortToInt(in.readShort()));
        samplingDto.setTotalMeasure(DecoderUtil.shortToInt(in.readShort()));
        samplingDto.setAccumulativeTotal(retaindecimal.divideHundred(DecoderUtil.shortToInt(in.readShort())));
        samplingDto.setType(DecoderUtil.toHex(in.readByte()));


        samplingDto.setTime(DecoderUtil.getBCDDate(in));
        super.decode(in,soketCommonDto);
    }


     public void operate( ){
         SokectBiz sokectBiz=(SokectBiz) ContextUtil.getBean(SokectBiz.class);
         sokectBiz.samplingRecive(this);

    }
}
